package com.esp.soccertutor.ui.adpater;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.esp.soccertutor.R;
import com.esp.soccertutor.ui.bean.BeanPurchase;
import com.esp.soccertutor.util.Const;

public class MyPurchaseAdapter extends ArrayAdapter<BeanPurchase>{
	
	private ImageLoader mLoader;
	private ArrayList<BeanPurchase> mArryLstBeanPurchase;
	private Context mContext;
	private int TypeID;
	static class ViewHolder {
		
		protected ImageView IndicateImage;
		protected ImageView mImageView;
		protected TextView txtView_Desc;
		protected Button btnDownload,btnPlayRead;
	}
	
	
	public MyPurchaseAdapter(Context context,ArrayList<BeanPurchase> object,int TypeID) {
		super(context,R.layout.row_purchase,object);
		mContext = context;
		mArryLstBeanPurchase = object;
		this.TypeID = TypeID;
		mLoader = new ImageLoader(context);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View mView = null;
		if (convertView == null) {
			
				mView = LayoutInflater.from(mContext).inflate(R.layout.row_purchase,null);
				final ViewHolder viewHolder = new ViewHolder();
				viewHolder.IndicateImage = (ImageView)mView.findViewById(R.id.IndicateImage);
				viewHolder.txtView_Desc = (TextView)mView.findViewById(R.id.txtView_Desc);
				viewHolder.mImageView = (ImageView)mView.findViewById(R.id.soccerImage);
				viewHolder.btnDownload = (Button)mView.findViewById(R.id.btn_red);
				viewHolder.btnPlayRead = (Button)mView.findViewById(R.id.btn_Blue);
				mView.setTag(viewHolder);
				
		}else {
			mView = convertView;
		}
		
		BeanPurchase mBean = mArryLstBeanPurchase.get(position);
		ViewHolder holder = (ViewHolder) mView.getTag();
		holder.txtView_Desc.setText(mBean.getDesc());
		if(this.TypeID == Const.TYPEID_EBOOKS){
			holder.IndicateImage.setBackgroundResource(R.drawable.ebook);
		}else{
			holder.IndicateImage.setBackgroundResource(R.drawable.video);
		}
			
		
		mLoader.DisplayImage(mBean.getImageURL(),holder.mImageView);
		if(mBean.isDownload){
			
			holder.btnDownload.setVisibility(View.GONE);
			String lbl = this.TypeID == Const.TYPEID_EBOOKS ? "Read" : "Play";
			holder.btnPlayRead.setText(lbl);
			
		}else{
			//TODO IS Download No Then Show Download Button
			holder.btnPlayRead.setVisibility(View.GONE);
		}
	
		
		return mView;
	}

}
